package com.umeox.ota.ui.filepicker;

import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;

import com.umeox.ota.FilePickerViewBinding;
import com.umeox.ota.R;
import com.umeox.ota.mvvm.viewModel.PickFileViewModel;
import com.umeox.ota.ui.activity.BaseActivity;
import com.umeox.ota.ui.filepicker.data.FilePickerConst;
import com.umeox.ota.ui.filepicker.engine.PickerManager;
import com.umeox.ota.ui.filepicker.fragment.DocFragment;
import com.umeox.ota.ui.filepicker.utils.FragmentUtil;
import com.umeox.ota.utils.ThemeColor;
import com.umeox.ota.utils.ViewUitls;

import java.util.ArrayList;

/**
 * Created by fighter_lee on 2017/7/28.
 */

public class FilePickerActivity extends BaseActivity<FilePickerViewBinding, PickFileViewModel> implements DocFragment.DocFragmentListener, View.OnFocusChangeListener, View.OnClickListener {
    private int type;
    private ImageView toolbarBack;
    private ImageView toolbarSetting;
    private TextView iotFilePickerTitle;

    protected void openSpecificFragment(int type, @Nullable ArrayList<String> selectedPaths) {
        if (PickerManager.getInstance().isDocSupport()) {
            PickerManager.getInstance().addDocTypes();
        }

        switch (type) {
            case FilePickerConst.DOC_PICKER:
//                DocFragment docFragment = DocFragment.newInstance(PickerManager.getInstance().getFileTypes(), Environment.getExternalStorageDirectory().getAbsolutePath());
                DocFragment docFragment = DocFragment.newInstance(new int[]{DocFragment.FILE_PATH_TYPE_SDCARD, DocFragment.FILE_PATH_TYPE_USB});
                FragmentUtil.addFragment(this, R.id.container, docFragment);
                break;
            default:
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (!FragmentUtil.havaFragment(this)) {
                finish();
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onBackPressed() {
        if (!FragmentUtil.havaFragment(this)) {
            finish();
        }
        super.onBackPressed();
    }

    private void returnData(ArrayList<String> paths) {
        Intent intent = new Intent();
        intent.putStringArrayListExtra(FilePickerConst.KEY_SELECTED_FILES, paths);
        setResult(RESULT_OK, intent);
        finish();
    }

    @Override
    public void onItemSelected() {
        if (PickerManager.getInstance().getMaxCount() == 1) {
            returnData(PickerManager.getInstance().getSelectedFiles());
        }
    }

    @Override
    public void onDirSelected(String path) {
        DocFragment docFragment = DocFragment.newInstance(PickerManager.getInstance().getFileTypes(), path);
        FragmentUtil.replaceFragment(this, R.id.container, docFragment);
    }

    @Override
    protected int genLayoutId(Bundle savedInstanceState) {
        genViewModel();
        return R.layout.activity_file_picker;
    }

    @Override
    public void genViewModel() {
        mViewModel = ViewModelProviders.of(this, mViewModelFactory).get(PickFileViewModel.class);
    }


    @Override
    protected void initData(Bundle savedInstanceState) {
        Intent intent = getIntent();
        if (intent != null) {
            final androidx.appcompat.widget.Toolbar toolbar = (androidx.appcompat.widget.Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
            getSupportActionBar().setDisplayShowTitleEnabled(false);

            toolbarBack = (ImageView) findViewById(R.id.toolbar_back);
            toolbarSetting = (ImageView) findViewById(R.id.toolbar_setting);
            iotFilePickerTitle = (TextView) findViewById(R.id.iot_main_title);

            toolbarBack.setImageDrawable(ViewUitls.tintDrawable(
                    getResources().getDrawable(R.mipmap.ic_back),
                    ColorStateList.valueOf(Color.WHITE)));
            toolbarSetting.setImageResource(R.mipmap.ic_selected);
            iotFilePickerTitle.setText(getResources().getString(R.string.local_update));

            type = intent.getIntExtra(FilePickerConst.EXTRA_PICKER_TYPE, FilePickerConst.DOC_PICKER);
            ArrayList<String> selectedPaths = intent.getStringArrayListExtra(FilePickerConst.KEY_SELECTED_FILES);

            if (selectedPaths != null && selectedPaths.size() > 0) {
                if (PickerManager.getInstance().getMaxCount() == 1) {
                    selectedPaths.clear();
                }
                PickerManager.getInstance().add(selectedPaths, FilePickerConst.FILE_TYPE_DOCUMENT);
            } else {
                selectedPaths = new ArrayList<>();
            }
            openSpecificFragment(type, selectedPaths);

            toolbarBack.setOnFocusChangeListener(this);
            toolbarSetting.setOnFocusChangeListener(this);

            toolbarBack.setOnClickListener(this);
            toolbarSetting.setOnClickListener(this);
        }
        onFocusChange(mBinding.pickFileView, true);
    }

    @Override
    protected boolean translucentStatusBar() {
        return true;
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        toolbarBack.setFocusable(true);
        toolbarSetting.setFocusable(true);
        switch (v.getId()) {
            case R.id.toolbar_back:
            case R.id.toolbar_setting:
                if (hasFocus) {
                    v.setBackgroundColor(ViewUitls.darkenColor(ThemeColor.backgroundColor));
                } else {
                    v.setBackgroundColor(ThemeColor.backgroundColor);
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.toolbar_back:
                onBackPressed();
                break;
            case R.id.toolbar_setting:
                returnData(PickerManager.getInstance().getSelectedFiles());
                break;
            default:
                break;
        }
    }
}
